package com.ms.oschina.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseFragment extends Fragment {

	public boolean isLoadData;
	private View rootView;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// rootView = UiUtils.inflate(getLayoutId());

		if (rootView == null) {
			// 只有给充气筒设置container才可以使用activity的主题。
			rootView = inflater.inflate(getLayoutId(), container, false);
			initView(rootView);
		}
		// 判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
		ViewGroup parent = (ViewGroup) rootView.getParent();
		if (parent != null) {
			parent.removeView(rootView);
		}

		return rootView;
	}

	public int getLayoutId() {
		return 0;
	}


	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		initData();
		initEvent();
		super.onActivityCreated(savedInstanceState);
	}

	/**
	 * 触发加载数据
	 */
	protected void initData() {
	}

	public abstract void initView(View view);

	protected void initEvent() {

	}

}
